package imaps;

import javax.mail.internet.*;
import javax.mail.*;
import java.util.*;
import javax.activation.*;


public class Conectar {
    
    
    public String email;
    public String password;

    final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    Properties props = System.getProperties();
    private Store store = null;
    private Folder activeFolder = null;
    

    public Conectar(String email, String password) {
        this.email = email;
        this.password = password;
    }
    
    public void connect()
       throws Exception {
    	
    	  props.setProperty("mail.store.protocol","imaps");
          Session session = Session.getInstance(props,null);
          store = session.getStore("imaps");
          store.connect("imap.gmail.com", email, password);
          activeFolder = store.getFolder("INBOX");
          activeFolder.open(Folder.READ_WRITE);
          //System.err.println(activeFolder.getMessageCount());
            
    }
    
 
    
    public void disconnect() throws Exception{
       if (store != null) {
          activeFolder.close(true);
          store.close();
       }
    }
    
    public List listMails() throws Exception{
    	
    	int total=0;
    	activeFolder = store.getFolder("INBOX");
        activeFolder.open(Folder.READ_WRITE);
    	total=activeFolder.getMessageCount();
    	//if(activeFolder.getNewMessageCount()>0)
    	List lista = ReadMail.getMails(activeFolder.getMessages(total,total));
    		
    	
         return lista;
    }
}